package com.test;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletSession;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;


import com.hotel.test.model.Booking;
import com.hotel.test.model.RomType;
import com.hotel.test.model.TblComment;
import com.hotel.test.model.impl.BookingImpl;
import com.hotel.test.service.BookingLocalServiceUtil;
import com.hotel.test.service.HotelLocalServiceUtil;
import com.hotel.test.service.TblCommentLocalServiceUtil;
import com.hotel.test.service.persistence.HotelUtil;
import com.hotel.test.service.persistence.RomTypeUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Hotel
 */
public class Hotel extends MVCPortlet {
	
	
	public void HotelItem (ActionRequest request, ActionResponse response) throws NumberFormatException, PortalException, SystemException, IOException, PortletException
	{
		String hotelId = request.getParameter("hotelId");
		String path = "/html/hotel/hotel_item.jsp";
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		
		addToPath(sbuff, "path", path);
		addToPath(sbuff, "hotelId", hotelId);
		//System.out.println("sbuff = "+ sbuff.toString());
		response.sendRedirect(sbuff.toString());
		
		
		 
	}
	public void bookAction (ActionRequest request, ActionResponse response) throws IOException
	{
		
		String path = "/html/hotel/booking.jsp";
		
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		addToPath(sbuff, "path", path);
		//System.out.println("sbuff = "+ sbuff.toString());
		response.sendRedirect(sbuff.toString());
	}
	
	public void addBooking (ActionRequest request, ActionResponse response) throws SystemException
	{
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		long userId = themeDisplay.getUserId();
		Booking book = new BookingImpl();
		book.setUserId(userId);
		book.setCreateDate(new Date());
		book.setModifiedDate(new Date());
		book.setNamePersonBooking(ParamUtil.getString(request, "namePersonBooking"));
		book.setNumberPhone(ParamUtil.getString(request, "numberPhone"));
		book.setEmailBooking(ParamUtil.getString(request, "email"));
		book.setAddress(ParamUtil.getString(request, "address"));
		book.setNumberOfGuest(ParamUtil.getInteger(request, "numberOfGuest"));
		book.setDayStart(ParamUtil.getString(request, "dayStart"));
		book.setDayFinish(ParamUtil.getString(request, "dayFinish"));
		book.setHotelId(ParamUtil.getLong(request, "hotelId"));
		book.setRomId(ParamUtil.getLong(request, "roomId"));
		BookingLocalServiceUtil.addBooking(book);
		
		
		
		
	}
	
	
	
	
	
	public void addToPath(StringBuffer sbuff, String name, String value)		  {
			if(!sbuff.toString().contains("?"))
			{
				sbuff.append("?" + name + "=");
			}
			else
				sbuff.append("&" + name + "=");
		
			
			sbuff.append(value);
		
	}
	
	

	public void removeExtraPath(RenderRequest request, String path) {
		System.out.println("nhay toi ham remove Path");
		String url = PortalUtil.getCurrentURL(request);
		//System.out.println("trong removePath url = "+ url);
		if(url.lastIndexOf("?")!=-1)
		{
			url = url.substring(0, url.indexOf("?"));
		}
		

		request.setAttribute("url", url.toString());
		System.out.println(url);
	}
	
	
	
	
	
	

	
	
	@Override
	public void doView(RenderRequest request, RenderResponse response) {
		String url = (String) PortalUtil.getCurrentURL(request);
		System.out.println("url = "  + url);
		String path = "";
		String hotelId = "";
		if(url.indexOf("hotelId")!=-1)
		{
			hotelId = url.substring(url.indexOf("hotelId"), url.length());
			if(hotelId.indexOf("&")!= -1)
				hotelId = hotelId.substring(hotelId.indexOf("=")+1,hotelId.indexOf("&"));
			else 
				hotelId = hotelId.substring(hotelId.indexOf("=")+1,hotelId.length());
		
		}
		
		if(url.indexOf("path=")!= -1)
		{
			path = url.substring(url.indexOf("path"), url.length());
			if(path.indexOf("&")!=-1)
				path = path.substring(path.indexOf("=")+1, path.indexOf("&"));
			else 
				path = path.substring(path.indexOf("=")+1, path.length());
		}
		
		if(!hotelId.equals("") && path.equals(""))
		{
			path = "/html/hotel/hotel_item.jsp";
			path +="?hotelId="+hotelId;
		}
		else if(!hotelId.equals("")&& !path.equals(""))
		{
			path = "/html/hotel/booking.jsp";
			path +="?hotelId="+hotelId;
		}
		else if(hotelId.equals("") && path.equals(""))
		{
			path = "/html/hotel/view.jsp";
		}
		
		removeExtraPath(request, path);
		
		try {
			include(path, request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 

}
